@file:Suppress("SENSELESS_COMPARISON")

package edu.njfu.networkdemo

import android.graphics.BitmapFactory
import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import edu.njfu.networkdemo.adapter.StudentAdapter
import edu.njfu.networkdemo.model.ResultVO
import edu.njfu.networkdemo.model.Student
import edu.njfu.networkdemo.utils.HttpUtil
import kotlinx.android.synthetic.main.activity_second.*
import okhttp3.Call
import okhttp3.Callback
import okhttp3.Response
import java.io.IOException
import java.net.URL

class SecondActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_second)
        var students:List<Student>?=null
        var linearLayoutManager = LinearLayoutManager(this)
        HttpUtil.sendOkHttpRequest("http://106.15.192.167:8080/listStudents",
              object: Callback {
                override fun onFailure(call: Call, e: IOException) {
                    Log.e("MainActivity","发生错误了")
                }
                override fun onResponse(call: Call, response: Response) {
                    val resultData=response.body?.string()
                    val type=object: TypeToken<List<Student>>(){}.type
                    students=Gson().fromJson<List<Student>>(resultData,type)
                    //创建线性布局
                    rv_students.layoutManager=linearLayoutManager
                    if(students!=null) {
                        val adapter = StudentAdapter(students!!);
                        rv_students.adapter = adapter
                    }
                } })

    }
}